package leetcode.code0463;

public class Solution {
	public int islandPerimeter(int[][] grid) {
		int[] d = new int[] { 0, -1, 0, 1, -1, 0, 1, 0 };
		int m = grid.length, n = grid[0].length;
		int ans = 0;
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				int cur = grid[i][j];
				if (cur == 1) {
					for (int k = 0; k < d.length; k += 2) {
						int x = i + d[k], y = j + d[k + 1];
						if (x < 0 || x == m || y < 0 || y == n || grid[x][y] == 0) {
							ans++;
						}
					}
				}
			}
		}
		return ans;
	}
}
